/*
 * Copyright 2018 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.armscomponent.app.mvp.ui.activity;

import static me.jessyan.armscomponent.app.mvp.ui.util.BlueToothUtil.PERMISSION_REQUEST_CONSTANT;
import static me.jessyan.armscomponent.app.mvp.ui.util.BlueToothUtil.REQUEST_BLUETOOTH_SCAN_RESULT;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.mvp.ui.adapter.DeviceInfoArrayAdapter;
import me.jessyan.armscomponent.app.mvp.ui.model.DeviceInformation;
import me.jessyan.armscomponent.app.mvp.ui.util.BlueToothUtil;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

@Route(path = RouterHub.APP_SCANDEVICEACTIVITY)
public class ScanDeviceActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView mTitle;
    @BindView(R.id.list)
    ListView mList;

    private List<DeviceInformation> mDatas;
    private DeviceInfoArrayAdapter mAdapter;
    private Context mContext;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_scan;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        //安卓6.0开始需要动态申请权限
        mContext = this;
        if (Build.VERSION.SDK_INT >= 6.0) {
            ActivityCompat.requestPermissions(this, new String[]{
                            Manifest.permission.ACCESS_FINE_LOCATION},
                   PERMISSION_REQUEST_CONSTANT);
        }
        mTitle.setText("扫描设备");
        mDatas = new ArrayList<>();
        mAdapter = new DeviceInfoArrayAdapter(mDatas,this);
        mList.setAdapter(mAdapter);

        //设备列表item的点击事件
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                BlueToothUtil.getInstance(mContext).cancelBluetooth();
                //获取点击的item的设备信息
                DeviceInformation deviceInformation = mDatas.get(position);
                //跳转到设备通信页面
                Intent intent = new Intent();
                //将设备地址传递过去
                intent.putExtra("name",deviceInformation.getDeviceName());
                intent.putExtra("address",deviceInformation.getDeviceAddress());
                setResult(REQUEST_BLUETOOTH_SCAN_RESULT,intent);
                onBackPressed();
            }
        });
        BlueToothUtil.getInstance(this).openBluetooth();
        BlueToothUtil.getInstance(this).initReceiver(mDatas,mAdapter);
        //搜索设备
        BlueToothUtil.getInstance(this).discoverBluetooth();
    }

    @OnClick({R.id.iv_back})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

}
